package org.example.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;

import org.example.domain.po.DepotRequest;
import org.example.domain.po.ErpDepotHead;
import org.example.domain.po.ErpUser;
import org.example.domain.po.Purchase;
import org.example.domain.po.*;
import org.example.domain.vo.ErpDepotHeadPurchaseVo;
import org.example.domain.vo.ErpDepotHeadRetailVo;
import org.example.domain.vo.ErpDepotItemVO;
import org.example.mapper.ErpDepotItemMapper;
import org.example.service.ErpDepotHeadService;
import org.example.mapper.ErpDepotHeadMapper;
import org.example.service.ErpDepotItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.example.service.ErpMaterialCurrentStockService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author 24059
 * @description 针对表【erp_depot_head(单据主表)】的数据库操作Service实现
 * @createDate 2024-10-17 11:02:58
 * @createDate 2024-10-17 11:02:58
 */
@Service
public class ErpDepotHeadServiceImpl extends ServiceImpl<ErpDepotHeadMapper, ErpDepotHead>
        implements ErpDepotHeadService{
    @Resource
    private ErpDepotItemMapper erpDepotItemMapper;

    @Resource
    private ErpMaterialCurrentStockService materialCurrentStockService;

    public Page<ErpDepotHead> selectByExample(Page<ErpDepotHead> page, @Param("number")String number, @Param("color")String color, @Param("createTime")String createTime, @Param("operTime")String operTime, @Param("username")String username, @Param("name")String name, @Param("status")String status, @Param("remark")String remark, @Param("standard")String standard,@Param("supName") String supName,@Param("tenantId") Long tenantId){
        return baseMapper.selectByExample(page, number, color, createTime, operTime, username, name, status, remark, standard, supName, tenantId);
    }

    public int updateOrderStatus(List<Integer> ids, String status){

        return baseMapper.updateOrderStatus(ids, status);
    }

    public boolean update(List<Integer> ids, String status){
        return lambdaUpdate().set(ErpDepotHead::getStatus, status).in(ErpDepotHead::getId, ids).update();
    }
    
    public int insertdd(ErpDepotHead erpDepotHead){
        return baseMapper.insertdd(erpDepotHead);
    }
    public Page<ErpDepotHead> selectOderck(Page<ErpDepotHead> page, @Param("number")String number,
                                           @Param("accountname")String accountname,
                                           @Param("color")String color, @Param("createTime")String createTime,
                                           @Param("operTime")String operTime, @Param("username")String username,
                                           @Param("supName")String supName, @Param("status")String status,
                                           @Param("remark")String remark,
                                           @Param("standard")String standard,@Param("supplier") String supplier,@Param("changeAmount")String changeAmount,@Param("dname")String dname,@Param("debt")String debt,@Param("tenantId") Long tenantId){
        return baseMapper.selectOderck(page, number, accountname, color, createTime, operTime, username, supName, status, remark, standard, supplier,changeAmount,dname,debt,tenantId);
    }

    public Page<ErpDepotHead> findPage(Page<ErpDepotHead> erpDepotHead,
                                       @Param("number") String number,
                                       @Param("status") String status,
                                       @Param("type") String type,
                                       @Param("subType") String subType,
                                       @Param("depId")Long depId,
                                       @Param("organId")Long organId,
                                       @Param("userId")Long userId,
                                       @Param("createTime")String createTime,
                                       @Param("endTime")String endTime,
                                       @Param("model")String model
    ){
        return baseMapper.findPage(erpDepotHead,number,status,type,subType,depId,organId,userId,createTime,endTime,model);
    }


    public Page<ErpDepotHead> selectOderth(Page<ErpDepotHead> page, @Param("number")String number,@Param("discountLastMoney") String discountLastMoney,
                                           @Param("accountname")String accountname,
                                           @Param("color")String color, @Param("createTime")String createTime,
                                           @Param("operTime")String operTime, @Param("username")String username,
                                           @Param("supName")String supName, @Param("status")String status,
                                           @Param("remark")String remark,
                                           @Param("standard")String standard,@Param("supplier") String supplier,@Param("changeAmount")String changeAmount,@Param("dname")String dname,@Param("debt")String debt,@Param("tenantId") Long tenantId){
        return baseMapper.selectOderth(page, number, discountLastMoney,accountname, color, createTime, operTime, username, supName, status, remark, standard, supplier,changeAmount,dname,debt,tenantId);
    }

    public boolean updateDelete(List<Integer> ids, String status) {
        return lambdaUpdate().set(ErpDepotHead::getDeleteFlag, status).in(ErpDepotHead::getId, ids).update();
    }
    
    

    public boolean updateStatus(List<Integer> ids, String status) {
        return lambdaUpdate().set(ErpDepotHead::getStatus, status).in(ErpDepotHead::getId, ids).update();
    }

    public Long finbyid(@Param("number")String number){
        return baseMapper.finbyid(number);
    }

    @Override
    public int updateOrder(ErpDepotHead erpDepotHead, Long id) {
        return 0;
    }

//    @Override
//    public Page<ErpDepotHead> selectOut(Page<ErpDepotHead> page, String number, String createTime, String operTime, String standard) {
//        return null;
//    }

    public Page<ErpDepotHead>selectOut(Page<ErpDepotHead> page, @Param("number")String number,
                                       @Param("createTime")String createTime, @Param("operTime")String operTime,@Param("standard")String standard,@Param("id") Long id,@Param("subType") String subType,@Param("tenantId") Long tenantId){
        return baseMapper.selectOut(page, number, createTime, operTime, standard,id,subType,tenantId);
    }
    public Page<ErpDepotHead>selectOut2(Page<ErpDepotHead> page, @Param("number")String number,
                                        @Param("createTime")String createTime, @Param("operTime")String operTime,@Param("standard")String standard,@Param("organId") Long organId){
        return baseMapper.selectOut2(page, number, createTime, operTime, standard,organId);
    }
    public Page<ErpDepotHead>RetailOut(Page<ErpDepotHead> page, @Param("number")String number,
                                       @Param("createTime")String createTime, @Param("operTime")String operTime,@Param("standard")String standard,@Param("id") Long id,@Param("subType") String subType){
        return baseMapper.RetailOut(page, number, createTime, operTime, standard,id,subType);
    }
    public Page<ErpDepotHead>selectOutth(Page<ErpDepotHead> page, @Param("number")String number,
                                         @Param("createTime")String createTime, @Param("operTime")String operTime,@Param("standard")String standard,@Param("id") Long id){
        return baseMapper.selectOutth(page, number, createTime, operTime, standard,id);
    }
    public int insertck(ErpDepotHead erpDepotHead){
        return baseMapper.insertck(erpDepotHead);
    }
    public List<ErpDepotHead>findherfid(@Param("headerId")Long headerId){
        return baseMapper.findherfid(headerId);
    }
    public int updateOrderStatus2(@Param("linkNumber") String linkNumber){
        return baseMapper.updateOrderStatus2(linkNumber);
    }
    public Long lookByHeaderId(@Param("number")String number){
        return baseMapper.lookByHeaderId(number);
    }
    public int updateOrderStatus3(@Param("linkNumber") String linkNumber){
        return baseMapper.updateOrderStatus3(linkNumber);
    }
    @Override
    public Page<ErpDepotHeadPurchaseVo> PurchaseFindByPage(Page<ErpDepotHeadPurchaseVo> page, Purchase purchase, Long tenantId) {
        Page<ErpDepotHeadPurchaseVo> erpDepotHeadPurchaseVoPage = baseMapper.PurchaseFindByPage(page, purchase, tenantId);
        List<ErpDepotHeadPurchaseVo> records = erpDepotHeadPurchaseVoPage.getRecords();
        for (ErpDepotHeadPurchaseVo record: records) {
            BigDecimal totalPrice = record.getTotalPrice();
            BigDecimal changeAmount = record.getChangeAmount();
            // 使用BigDecimal的compareTo方法与BigDecimal.ZERO进行比较
            if (totalPrice.compareTo(BigDecimal.ZERO) < 0) {
                //.abs()获取绝对值
                record.setChangeAmount(changeAmount.abs());
                record.setTotalPrice(totalPrice.abs());
            }
        }
        return erpDepotHeadPurchaseVoPage;
    }
    public int deprotinsert(ErpDepotHead erpDepotHead){
        return baseMapper.deprotinsert(erpDepotHead);
    }

    @Override
    public int depotinserttwo(ErpDepotHead erpDepotHead) {
        return baseMapper.depotinserttwo(erpDepotHead);
    }

    /**
     * 采购单据添加
     * @param depotRequest
     * @param user
     * @return
     */
    @Override
    @Transactional
    public int insertPurchase(DepotRequest depotRequest, ErpUser user) {
        Long tenantId = user.getTenantId();
        ErpDepotHead erpDepotHead = depotRequest.getErpDepotHead();
        List<ErpDepotItem> erpDepotItems = depotRequest.getErpDepotItems();
        erpDepotHead.setTenantId(tenantId);
        erpDepotHead.setCreator(user.getId());
        //添加主单据
        int i = baseMapper.insertPurchase(erpDepotHead);
        //获取生成主单据的id
        Long headerId = baseMapper.finbyid(erpDepotHead.getNumber());
        //关联编号
        String linkNumber = erpDepotHead.getLinkNumber();
        //获取关联单据id
        Long linkNumberId = baseMapper.finbyid(linkNumber);
        //获取关联子单据数据
        List<ErpDepotItemVO> items = erpDepotItemMapper.findItems(linkNumberId);
        //判断修改被关联主单据的状态
        if (linkNumber != null){
            if (erpDepotItems.size() == items.size()) {
                baseMapper.updateOrderStatus2(linkNumber);
            } else {
                baseMapper.updateOrderStatus3(linkNumber);
            }
        }
        //添加子单据信息
        if (i > 0){
            for (ErpDepotItem erpDepotItem : erpDepotItems){
                erpDepotItem.setHeaderId(headerId);
                erpDepotItem.setTenantId(tenantId);
                erpDepotItemMapper.insert(erpDepotItem);
                materialCurrentStockService.updateStock(erpDepotItem.getMaterialId(), erpDepotItem.getDepotId(), erpDepotItem.getOperNumber(), tenantId);
            }
        }
        return i;
    }

    public Page<ErpDepotHeadRetailVo>select1(Page<ErpDepotHeadRetailVo> page, @Param("retail") Retail retail,Long tenantId){
        return baseMapper.select1(page,retail,tenantId);
    }
    public Page<ErpDepotHeadRetailVo>select2(Page<ErpDepotHeadRetailVo> page, @Param("retail") Retail retail,Long tenantId){
        return baseMapper.select2(page,retail,tenantId);
    }



    public int depotinsertdb(ErpDepotHead erpDepotHead) {

        return baseMapper.depotinsertdb(erpDepotHead);
    }
    public Page<ErpDepotHead> selectByqtrk(Page<ErpDepotHead> page){
        return baseMapper.selectByqtrk(page);

    }

    @Override
    public boolean modifyTheState(List<Integer> ids, String status) {
        return lambdaUpdate().set(ErpDepotHead::getStatus, status).in(ErpDepotHead::getId, ids).update();
    }

    public int updateDepotHead(@Param("depotHead") ErpDepotHead erpDepotHead,@Param("number")String number){
        return baseMapper.updateDepotHead(erpDepotHead,number);
    }
    public Page<ErpDepotHead> select3(Page<ErpDepotHead> page){
        return baseMapper.select3(page);
    }

    @Autowired
    ErpDepotItemService erpDepotItemService;

    @Override
    @Transactional
    public int deleteRetail(@Param("id") Long id) {
        erpDepotItemService.deleteItem(id);
        return baseMapper.deleteRetail(id);
    }

    @Override
   public int updateRetail4(@Param("id") Long id,@Param("status") String status){
        return baseMapper.updateRetail4(id,status);
    }



   public Long RetailInDetail(@Param("linkId") Long linkId){
        return baseMapper.RetailInDetail(linkId);
    }
    public Long RetailIn(@Param("id") Long id){
        return baseMapper.RetailIn(id);
    }
    public int updateRetail(@Param("id") Long id){
        return baseMapper.updateRetail(id);
    }
    public int updateRetail3(@Param("id") Long id){
        return baseMapper.updateRetail3(id);
    }

    public Long RetailInDetailty(@Param("id") Long id){
        return baseMapper.RetailInDetailty(id);
    }


    public String select4(@Param("id") Long id){
        return baseMapper.select4(id);
    }
    public Long  select5(@Param("number") String number){
        return baseMapper.select5(number);
    }
    public Long  select6(@Param("linkNumber") String linkNumber){
        return baseMapper.select6(linkNumber);
    }
    public Long  select7(@Param("id") Long id){
        return baseMapper.select7(id);
    }
    public List<Long> select8(@Param("headerId") Long headerId){
        return baseMapper.select8(headerId);
    }
    public Long  select9( @Param("linkIds") List<Long> linkIds){
        return baseMapper.select9(linkIds);
    }
    public String  select10(@Param("headerId") Long headerId){
        return baseMapper.select10(headerId);
    }

}




